package com.example.myjavafx.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.chart.*;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.StackPane;

public class ChartsController {

    @FXML
    private ComboBox<String> chartTypeComboBox;

    @FXML
    private StackPane chartContainer;

    // 图表类型枚举
    private enum ChartType {
        BAR_CHART("柱状图"),
        PIE_CHART("饼图"),
        LINE_CHART("折线图"),
        AREA_CHART("区域图"),
        SCATTER_CHART("散点图");

        private final String displayName;

        ChartType(String displayName) {
            this.displayName = displayName;
        }

        @Override
        public String toString() {
            return displayName;
        }
    }

    @FXML
    public void initialize() {
        // 初始化图表类型下拉框
        chartTypeComboBox.getItems().addAll(
                ChartType.BAR_CHART.toString(),
                ChartType.PIE_CHART.toString(),
                ChartType.LINE_CHART.toString(),
                ChartType.AREA_CHART.toString(),
                ChartType.SCATTER_CHART.toString()
        );

        // 设置默认选择
        chartTypeComboBox.setValue(ChartType.BAR_CHART.toString());

        // 添加选择监听器
        chartTypeComboBox.setOnAction(event -> updateChart());

        // 初始化显示默认图表
        updateChart();
    }

    @FXML
    private void refreshData() {
        // 刷新图表数据
        updateChart();
    }

    private void updateChart() {
        // 清除当前图表
        chartContainer.getChildren().clear();

        String selectedType = chartTypeComboBox.getValue();

        if (selectedType.equals(ChartType.BAR_CHART.toString())) {
            chartContainer.getChildren().add(createBarChart());
        } else if (selectedType.equals(ChartType.PIE_CHART.toString())) {
            chartContainer.getChildren().add(createPieChart());
        } else if (selectedType.equals(ChartType.LINE_CHART.toString())) {
            chartContainer.getChildren().add(createLineChart());
        } else if (selectedType.equals(ChartType.AREA_CHART.toString())) {
            chartContainer.getChildren().add(createAreaChart());
        } else if (selectedType.equals(ChartType.SCATTER_CHART.toString())) {
            chartContainer.getChildren().add(createScatterChart());
        }
    }

    private BarChart<String, Number> createBarChart() {
        CategoryAxis xAxis = new CategoryAxis();
        NumberAxis yAxis = new NumberAxis();

        xAxis.setLabel("产品类别");
        yAxis.setLabel("销售额");

        BarChart<String, Number> barChart = new BarChart<>(xAxis, yAxis);
        barChart.setTitle("产品销售统计");

        // 创建数据系列
        XYChart.Series<String, Number> series1 = new XYChart.Series<>();
        series1.setName("2023年");

        series1.getData().add(new XYChart.Data<>("电子产品", 25000));
        series1.getData().add(new XYChart.Data<>("家居用品", 20000));
        series1.getData().add(new XYChart.Data<>("食品饮料", 30000));
        series1.getData().add(new XYChart.Data<>("服装鞋帽", 15000));
        series1.getData().add(new XYChart.Data<>("图书音像", 10000));

        XYChart.Series<String, Number> series2 = new XYChart.Series<>();
        series2.setName("2022年");

        series2.getData().add(new XYChart.Data<>("电子产品", 22000));
        series2.getData().add(new XYChart.Data<>("家居用品", 18000));
        series2.getData().add(new XYChart.Data<>("食品饮料", 25000));
        series2.getData().add(new XYChart.Data<>("服装鞋帽", 17000));
        series2.getData().add(new XYChart.Data<>("图书音像", 12000));

        barChart.getData().addAll(series1, series2);

        return barChart;
    }

    private PieChart createPieChart() {
        ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
                new PieChart.Data("电子产品", 25),
                new PieChart.Data("家居用品", 20),
                new PieChart.Data("食品饮料", 30),
                new PieChart.Data("服装鞋帽", 15),
                new PieChart.Data("图书音像", 10)
        );

        PieChart pieChart = new PieChart(pieChartData);
        pieChart.setTitle("产品销售占比");
        pieChart.setLabelsVisible(true);

        return pieChart;
    }

    private LineChart<String, Number> createLineChart() {
        CategoryAxis xAxis = new CategoryAxis();
        NumberAxis yAxis = new NumberAxis();

        xAxis.setLabel("月份");
        yAxis.setLabel("销售额");

        LineChart<String, Number> lineChart = new LineChart<>(xAxis, yAxis);
        lineChart.setTitle("月度销售趋势");

        // 创建数据系列
        XYChart.Series<String, Number> series1 = new XYChart.Series<>();
        series1.setName("2023年");

        series1.getData().add(new XYChart.Data<>("1月", 23000));
        series1.getData().add(new XYChart.Data<>("2月", 25000));
        series1.getData().add(new XYChart.Data<>("3月", 24000));
        series1.getData().add(new XYChart.Data<>("4月", 28000));
        series1.getData().add(new XYChart.Data<>("5月", 26000));
        series1.getData().add(new XYChart.Data<>("6月", 30000));

        XYChart.Series<String, Number> series2 = new XYChart.Series<>();
        series2.setName("2022年");

        series2.getData().add(new XYChart.Data<>("1月", 20000));
        series2.getData().add(new XYChart.Data<>("2月", 22000));
        series2.getData().add(new XYChart.Data<>("3月", 21000));
        series2.getData().add(new XYChart.Data<>("4月", 25000));
        series2.getData().add(new XYChart.Data<>("5月", 23000));
        series2.getData().add(new XYChart.Data<>("6月", 27000));

        lineChart.getData().addAll(series1, series2);

        return lineChart;
    }

    private AreaChart<String, Number> createAreaChart() {
        CategoryAxis xAxis = new CategoryAxis();
        NumberAxis yAxis = new NumberAxis();

        xAxis.setLabel("季度");
        yAxis.setLabel("销售额");

        AreaChart<String, Number> areaChart = new AreaChart<>(xAxis, yAxis);
        areaChart.setTitle("季度销售趋势");

        // 创建数据系列
        XYChart.Series<String, Number> series1 = new XYChart.Series<>();
        series1.setName("2023年");

        series1.getData().add(new XYChart.Data<>("Q1", 75000));
        series1.getData().add(new XYChart.Data<>("Q2", 85000));
        series1.getData().add(new XYChart.Data<>("Q3", 80000));
        series1.getData().add(new XYChart.Data<>("Q4", 95000));

        XYChart.Series<String, Number> series2 = new XYChart.Series<>();
        series2.setName("2022年");

        series2.getData().add(new XYChart.Data<>("Q1", 65000));
        series2.getData().add(new XYChart.Data<>("Q2", 70000));
        series2.getData().add(new XYChart.Data<>("Q3", 75000));
        series2.getData().add(new XYChart.Data<>("Q4", 80000));

        areaChart.getData().addAll(series1, series2);

        return areaChart;
    }

    private ScatterChart<Number, Number> createScatterChart() {
        NumberAxis xAxis = new NumberAxis(0, 10, 1);
        NumberAxis yAxis = new NumberAxis(0, 100, 10);

        xAxis.setLabel("产品评分");
        yAxis.setLabel("销售量");

        ScatterChart<Number, Number> scatterChart = new ScatterChart<>(xAxis, yAxis);
        scatterChart.setTitle("产品评分与销售量关系");

        // 创建数据系列
        XYChart.Series<Number, Number> series1 = new XYChart.Series<>();
        series1.setName("电子产品");

        series1.getData().add(new XYChart.Data<>(7.5, 45));
        series1.getData().add(new XYChart.Data<>(8.2, 65));
        series1.getData().add(new XYChart.Data<>(9.0, 85));
        series1.getData().add(new XYChart.Data<>(6.8, 35));
        series1.getData().add(new XYChart.Data<>(7.0, 40));
        series1.getData().add(new XYChart.Data<>(8.5, 70));

        XYChart.Series<Number, Number> series2 = new XYChart.Series<>();
        series2.setName("家居用品");

        series2.getData().add(new XYChart.Data<>(6.5, 30));
        series2.getData().add(new XYChart.Data<>(7.2, 45));
        series2.getData().add(new XYChart.Data<>(8.0, 60));
        series2.getData().add(new XYChart.Data<>(8.8, 75));
        series2.getData().add(new XYChart.Data<>(7.8, 50));
        series2.getData().add(new XYChart.Data<>(9.2, 80));

        scatterChart.getData().addAll(series1, series2);

        return scatterChart;
    }
}
